home *** CD-ROM | disk | FTP | other *** search
/ Programmers Heaven 2 / Programmers Heaven 2.iso / files / graphics / library / wgt51_r2.zip / WGT5 / EXAMPLES / WGT69.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-03  |  7.8 KB  |  266 lines

  1. /*
  2. ==============================================================================
  3.               WordUp Graphics Toolkit Version 5.0                     
  4.                  Demonstration Program 68
  5.                                           
  6.  This program uses the scrolling library to create a parallax scrolling      
  7.  world with two layers.  The background layer is a 640x400 bitmap.
  8.  Use the arrow keys to move around and ESC will exit.                                                          
  9.                                           
  10.  *** PROJECT ***                                                             
  11.  This program requires the files WGT5_WC.LIB and WSCR_WC.LIB to be linked.   
  12.                                           
  13.  *** DATA FILES ***                                                          
  14.  PARAFRON.SPR, PARASPR.SPR, PARAMAPF.WMP, SHIP.PCX
  15.                                WATCOM C++ VERSION 
  16. ==============================================================================
  17. */
  18.  
  19. #include <stdlib.h>
  20. #include <dos.h>
  21. #include <wgt5.h>
  22. #include <wgtscrol.h>
  23.  
  24.  
  25. #define WIND_SIZEX 18           /* Set scrolling window dimensions (in tiles) */
  26. #define WIND_SIZEY 12
  27.  
  28. #define SPEED 8                 /* Speed of scrolling */
  29.  
  30. #define LEFT 75                 /* Keyboard codes */
  31. #define RIGHT 77
  32. #define UP 72
  33. #define DOWN 80
  34. #define ESC 1
  35. #define KEY_S 31
  36. #define KEY_L 38
  37.  
  38.  
  39. #define BACKWIN 0               /* The NORMAL layer */
  40. #define FRONTWIN 1              /* The PARALLAX layer */
  41.  
  42. block fronttiles[256];          /* Tiles, objects and tiles types for the */
  43. scrollsprite frontobject[200];  /* foreground window. */
  44. short fronttypes[256];
  45.  
  46. block sprites[50];              /* The sprite images */
  47.  
  48. color pal[256];
  49.  
  50. short movex, movey;             /* Amount to scroll the front */
  51. short backx, backy;             /* Coordinates of the background screen */
  52.  
  53. short x, y;                     /* Current viewing coordinates */
  54.                 /* The window tries to center itself on
  55.                    this coordinate. */
  56.  
  57. short num_ball_behind;          /* Number of yellow balls behind front layer.
  58.                    All yellow balls with object numbers 
  59.                    100-199 are drawn between the background 
  60.                    and foreground layers. */
  61. short num_ball_infront;         /* Number of yellow balls in front of every layer */
  62.  
  63. short cloud_speed[100];         /* Movement speed and direction for clouds */
  64.  
  65. short oldmode;                  /* Old video mode */
  66.  
  67. wgtmap frontmap;                /* Holds the scrolling map */
  68.  
  69. float xscale, yscale;           /* Difference between size of map and size
  70.                    of background picture */
  71.  
  72. block backgroundpic;            /* A 640x400 picture */
  73.  
  74. void find_sprites (void)
  75. /* Searches for the maximum object numbers for each of the three categories
  76.    of wobjects. (clouds, ball behind, ball in front) */
  77. {
  78.  short i;
  79.  
  80.  num_ball_behind = 0;
  81.  for (i = 100; i < 200; i++)
  82.    if (frontobject[i].on)
  83.       num_ball_behind = i;
  84.  
  85.  num_ball_infront = 0;
  86.  for (i = 0; i < 100; i++)
  87.    if (frontobject[i].on)
  88.       num_ball_infront = i;
  89. }
  90.  
  91.  
  92.  
  93.  
  94. void move_balls (void)
  95. /* Moves the balls around randomly */
  96. {
  97.   short i;
  98.   short maxx, maxy;
  99.  
  100.   maxx = mapwidth[BACKWIN] * tilewidth[BACKWIN];
  101.   maxy = mapheight[BACKWIN] * tileheight[BACKWIN];
  102.  
  103.   for (i = 0; i <= num_ball_infront; i++)
  104.   {
  105.     frontobject[i].x += (rand () % 17) - 8;    /* Randomly move the ball */
  106.     frontobject[i].y += (rand () % 17) - 8;
  107.  
  108.     if (frontobject[i].x < 0)                   /* Check the X boundaries */
  109.       frontobject[i].x = 0;
  110.     else if (frontobject[i].x > maxx)
  111.       frontobject[i].x = maxx;
  112.  
  113.     if (frontobject[i].y < 0)                   /* Check the Y boundaries */
  114.       frontobject[i].y = 0;
  115.     else if (frontobject[i].y > maxy)
  116.       frontobject[i].y = maxy;
  117.   }
  118.  
  119.   for (i = 100; i <= num_ball_behind; i++)
  120.    /* Do the same with the balls behind the front layer. */
  121.   {
  122.     frontobject[i].x += (rand () % 17) - 8;
  123.     frontobject[i].y += (rand () % 17) - 8;
  124.  
  125.     if (frontobject[i].x < 0)
  126.       frontobject[i].x = 0;
  127.     else if (frontobject[i].x > maxx)
  128.       frontobject[i].x = maxx;
  129.     if (frontobject[i].y < 0)
  130.       frontobject[i].y = 0;
  131.     else if (frontobject[i].y > maxy)
  132.       frontobject[i].y = maxy;
  133.   }
  134. }
  135.  
  136.  
  137. void main (void)
  138. {
  139.   struct dostime_t t1, t2;
  140.   float seconds, fps;
  141.   int frames = 0;  
  142.  
  143.   oldmode = wgetmode ();
  144.   if (!vgadetected ())
  145.   {
  146.     printf ("VGA is required to run this program...");
  147.     exit (1);
  148.   }
  149.  
  150.   printf ("WGT Example #69\n\n");
  151.   printf ("This program uses the scrolling library to create a parallax scrolling\n");
  152.   printf ("world with two layers.  The background layer is a 640x400 bitmap.\n");
  153.   printf ("Use the arrow keys to move around and ESC will exit.\n");
  154.   printf ("\nPress any key to begin.\n");
  155.   getch ();
  156.  
  157.   vga256 ();
  158.  
  159.   wloadsprites (pal, "paraspr.spr", sprites, 0, 49);
  160.   wloadsprites (pal, "parafron.spr", fronttiles, 0, 255);
  161.   backgroundpic = wloadpcx ("ship.pcx", pal);
  162.  
  163.   wsetpalette (0, 255, pal);
  164.  
  165.   winitscroll (BACKWIN, NORMAL, 0, WIND_SIZEX, WIND_SIZEY, fronttiles);
  166.   /* Make a background window using the same tile sizes. 
  167.      We won't use tiles on this layer but we still need to set up the
  168.      window with the correct size. */
  169.  
  170.   winitscroll (FRONTWIN, PARALLAX, BACKWIN, WIND_SIZEX, WIND_SIZEY, fronttiles);
  171.   /* Link this window to the background one */
  172.  
  173.   frontmap = wloadmap (FRONTWIN, "paramapf.wmp", fronttypes, frontobject);
  174.   wcopymap (FRONTWIN, BACKWIN);
  175.   /* Load the maps in for both windows */
  176.  
  177.   wshowwindow (FRONTWIN, 0, 0);
  178.   /* Set the initial viewing coordinates. */
  179.   x = 0 * tilewidth[FRONTWIN];
  180.   y = 0 * tileheight[FRONTWIN];
  181.  
  182.   xscale = (float)(wgetblockwidth (backgroundpic) - 
  183.        windowmaxx [FRONTWIN] - 1) / (float)worldmaxx [FRONTWIN];
  184.   yscale = (float)(wgetblockheight (backgroundpic) - 
  185.        windowmaxy [FRONTWIN] - 1) / (float)worldmaxy [FRONTWIN];
  186.  
  187.   find_sprites ();
  188.   
  189.   installkbd ();                 /* Install the custom keyboard handler */
  190.   _dos_gettime (&t1);
  191.   do {
  192.     move_balls ();
  193.  
  194.     if (kbdon[LEFT])             /* Change the viewing coordinates */
  195.     {
  196.       x -= 8;
  197.       if (x < 0)
  198.     x = 0;
  199.     }
  200.     if (kbdon[RIGHT])
  201.     {
  202.       x += 8;
  203.       if (x > worldmaxx[FRONTWIN]) 
  204.     x = worldmaxx[FRONTWIN];
  205.     }
  206.     if (kbdon[UP])
  207.     {
  208.       y -= 8;
  209.       if (y < 0) 
  210.     y = 0;
  211.     }
  212.     if (kbdon[DOWN])
  213.     {
  214.       y += 8;
  215.       if (y > worldmaxy[FRONTWIN]) 
  216.     y = worldmaxy[FRONTWIN];
  217.     }
  218.  
  219.     /* Move the foreground by finding the difference between the actual
  220.        world coordinates and the desired world coordinates.  This will
  221.        make sure the window is centered on the (x,y) coordinate. */
  222.     movex = x - worldx[FRONTWIN]; 
  223.     movey = y - worldy[FRONTWIN];
  224.  
  225.     
  226.     
  227.     backx = x * xscale;
  228.     backy = y * yscale;
  229.  
  230.     wsetscreen (scrollblock [BACKWIN]);
  231.     wputblock (-backx, -backy, backgroundpic, 0);
  232.     
  233.     wshowobjects(BACKWIN, 100, num_ball_behind, sprites, frontobject);
  234.     /* Show the balls that are behind the front layer. (They will be just in 
  235.        front of the clouds) */
  236.    
  237.     wscrollwindow (FRONTWIN, movex, movey);
  238.     /* Display the foreground layer */
  239.  
  240.     wshowobjects(FRONTWIN, 0, num_ball_infront, sprites, frontobject);
  241.     /* Show the balls that are in front of everything else. */
  242.  
  243.     wnormscreen ();
  244.     /* Uncomment the following line to time with the vertical retrace */
  245.     //wretrace();
  246.     
  247.     wputblock (0, 0, scrollblock[BACKWIN], 0);
  248.     /* Once the image is built, display the whole thing on the visual screen. */
  249.     
  250.     
  251.     frames++;
  252.   } while (kbdon[ESC] == 0);
  253.   _dos_gettime (&t2);
  254.  
  255.   /* Now clean up everything */
  256.   wendscroll (FRONTWIN);
  257.   wendscroll (BACKWIN);
  258.  
  259.   uninstallkbd ();
  260.   wfreeblock (backgroundpic);
  261.   wfreesprites (fronttiles, 0, 255);
  262.   wfreesprites (sprites, 0, 49);
  263.   wfreemap (frontmap);
  264.   wsetmode (oldmode);
  265. }
  266.